package org.samith.web.servlet;

import java.io.IOException;

import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.transaction.UserTransaction;

import org.samith.dao.AccountDao;
import org.samith.dao.ServiceTypeDao;
import org.samith.dao.SubscribedServiceDao;
import org.samith.dao.SubscriberDao;
import org.samith.dao.UserDao;
import org.samith.dao.UserProfileDao;
import org.samith.entity.Account;
import org.samith.entity.ServiceType;
import org.samith.entity.SubscribedService;
import org.samith.entity.Subscriber;
import org.samith.entity.User;
import org.samith.entity.UserProfile;
import org.samith.web.helper.DigestAlgorithm;
import org.samith.web.helper.WebUtil;

@SuppressWarnings("serial")
public class SignupServlet extends HttpServlet {

	@EJB
	UserProfileDao userProfileDao;
	@EJB
	UserDao userDao;
	@EJB
	SubscriberDao subscriberDao;
	@EJB
	AccountDao accountDao;
	@EJB
	SubscribedServiceDao subscribedServiceDaoDao;
	@EJB
	ServiceTypeDao serviceTypeDao;
	@Resource
	UserTransaction tx;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		ServletContext context = getServletConfig().getServletContext();
		context.getRequestDispatcher("/SignUp/index.jsp").forward(request,
				response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String initials = request.getParameter("initials");
		String email = request.getParameter("email");
		String address = request.getParameter("address");
		String firstname = request.getParameter("firstName");
		String lastname = request.getParameter("lastName");
		String occupation = request.getParameter("occupation");
		String nic = request.getParameter("nic");
		String title = request.getParameter("title");
		String mobile = request.getParameter("mobile");
		
		User newUser = new User();
		UserProfile newUserProfile = new UserProfile();

		newUser.setIsActive(new Byte("0"));
		newUser.setPhoneNumber(mobile);
		newUser.setUserName(username);
		newUser.setAuthToken(WebUtil.generatePseudoRandomString());
		newUser.setPassword(DigestAlgorithm.getSHA1Digest(password));

		newUserProfile.setAddress(address);
		newUserProfile.setEmail(email);
		newUserProfile.setFirstName(firstname);
		newUserProfile.setLastName(lastname);
		newUserProfile.setInitials(initials);
		newUserProfile.setOccupation(occupation);
		newUserProfile.setTitle(title);
		newUserProfile.setNic(nic);
		newUserProfile.setUserId(newUser);

		Subscriber subscriber = new Subscriber();
		subscriber.setAddress(address);
		subscriber.setName(username);
		subscriber.setPhoneNumber(mobile);
		subscriber.setUserId(newUser);

		Account account = new Account();
		account.setCurrentBalance(0.0);
		account.setIsActive(new Byte("1"));
		account.setLastPaidAmount(0.0);
		account.setSubscriberId(subscriber);
		
		//User is by default entitled for LOCAL CALLS service. Add a new entry
		SubscribedService subscribedService = new SubscribedService();
		subscribedService.setIsActive(new Byte("1"));
		ServiceType service = serviceTypeDao.read(7L);
		subscribedService.setServiceId(service);
		subscribedService.setSubscriberId(subscriber);
		
		try {
			tx.begin();
			userDao.create(newUser);
			userProfileDao.create(newUserProfile);
			subscriberDao.create(subscriber);
			accountDao.create(account);
			subscribedServiceDaoDao.create(subscribedService);
			tx.commit();
			HttpSession session = request.getSession();
			session.setAttribute("verificationCode", newUser.getAuthToken());
			System.out.println("========== All saved =============");
		} catch (Exception e) {
			System.out
					.println("============= Exception in transaction execution ");
			e.printStackTrace();
		}
		response.sendRedirect("/ejbApp/verifyAccount");
	}
}
